clear all
clc

data = readtable('../../data/working/Sample.csv');
c = readtable('../../data/working/table5_col4.csv');

% sort in ascending order of household_id and then currency_id
data = sortrows(data,[2 3]);

data.supply = data.marketcap./(data.open);

ID_unique = unique(data.household_id);

unique_curr = unique(data.currency_id);

% for ii=1:size(data,1)
%
%     data.first_time2(ii) = isequal(data.first_time(ii),{'First Purchase: Late'});
%
% end

for ii=1:length(unique_curr)
    
    temp = unique_curr(ii);
    
    for nn=1:size(data,1)
        
        temp_ind = isequal(temp,data.currency_id(nn));
        
        if temp_ind==1
            
            data.currency_id2(nn) = ii;
            
        end
        
    end
    %ii
end

data.pow = ones(size(data,1),1);

for ii=[1 8]

    temp_ind = (data.currency_id2==ii);
   
    data.pow(temp_ind) = 0;
    
end

data.dep_hat = exp(c.logME*data.logME + ...
    c.short_increase*data.short_increase + ...
    c.long_decrease*data.long_decrease + ...
    c.currencies_potential*data.currencies_potential + ...
    c.incomenewdummy*data.incomenewdummy + c.agenewdummy*data.agenewdummy + ...
    c.countrynewdummy*data.countrynewdummy + ...
    c.type_objective*data.type_objective +...
    c.log_libor*data.log_libor + c.log_sp500*data.log_sp500 + c.cons +...
    c.pow*data.pow +...
    c.beta*data.beta + c.momentum4*data.momentum4);

data.w_hat = zeros(size(data,1),1);

for ii=1:length(ID_unique)
    
    temp_ind = find(data.household_id==ID_unique(ii));
        
    for ccu=1:length(unique_curr)
        
        data.w_hat(temp_ind(ccu)) = data.dep_hat(temp_ind(ccu))./(1+sum(data.dep_hat(temp_ind)));
        
    end
    
    data.buyer(temp_ind) = (sum(data.choice(temp_ind))>0);
    
end

data.mktcap_hat = data.w_hat.*data.wealth;

scalefactor_wave1 = zeros(length(unique_curr),1);
scalefactor_wave2 = zeros(length(unique_curr),1);
supply_wave1 = zeros(length(unique_curr),1);
supply_wave2 = zeros(length(unique_curr),1);

for ii=1:length(unique_curr)
    
    temp = (data.currency_id2==ii);
    
    mktcap_wave1_hat = nansum(data.mktcap_hat(temp&data.wave==1)); % there are NaNs her. WHY???
    mktcap_wave1 = nanmean(data.marketcap(temp&data.wave==1));
    scalefactor_wave1(ii) = mktcap_wave1/mktcap_wave1_hat;
    supply_wave1(ii) = nanmean(data.supply(temp&data.wave==1));
    
    mktcap_wave2_hat = sum(data.mktcap_hat(temp&data.wave==2));
    mktcap_wave2 = mean(data.marketcap(temp&data.wave==2));
    scalefactor_wave2(ii) = mktcap_wave2/mktcap_wave2_hat;
    supply_wave2(ii) = mean(data.supply(temp&data.wave==2));
    
end

residuals = data.dep_weighted./data.dep_hat;

%% compute equilibrium prices

fun1 = @(p) eq_p_fun(p,data,c,scalefactor_wave1,supply_wave1,1,ID_unique,unique_curr);
fun2 = @(p) eq_p_fun(p,data,c,scalefactor_wave2,supply_wave2,2,ID_unique,unique_curr);

p0_1 = zeros(length(unique_curr),1);
for ii=1:length(unique_curr)
    temp = (data.currency_id2==ii);
    p0_1(ii) = log(mean(data.open(temp&data.wave==1)));
end
[p_eq_1] = solve_contraction(fun1,p0_1);

p0_2 = zeros(length(unique_curr),1);
for ii=1:length(unique_curr)
    temp = (data.currency_id2==ii);
    p0_2(ii) = log(mean(data.open(temp&data.wave==2)));
end
[p_eq_2] = solve_contraction(fun2,p0_2);

% (exp(p0_1) - exp(p_eq_1))./exp(p_eq_1)
% (exp(p0_2) - exp(p_eq_2))./exp(p_eq_2)

%% compute equilibrium allocations

mktcap_hat_1 = zeros(length(unique_curr),1);
mktcap_hat_2 = zeros(length(unique_curr),1);

mktcap_hat_sum_1 = zeros(length(unique_curr),1);
mktcap_hat_sum_2 = zeros(length(unique_curr),1);

for ii=1:length(unique_curr)
    
    temp = (data.currency_id2==ii);
    
    mktcap_hat_1(ii) = nanmedian(data.mktcap_hat(temp&data.wave==1));
    mktcap_hat_2(ii) = nanmedian(data.mktcap_hat(temp&data.wave==2));
    
    mktcap_hat_sum_1(ii) = nansum(data.mktcap_hat(temp&data.wave==1));
    mktcap_hat_sum_2(ii) = nansum(data.mktcap_hat(temp&data.wave==2));
        
end

cash_all = NaN(length(ID_unique),1);
wave_vec = NaN(length(ID_unique),1);

for ii=1:length(ID_unique)

    temp = (data.household_id==ID_unique(ii));
    
    temp_sum_w = nansum(data.w_hat(temp));

    wealth_temp = unique(data.wealth(temp));
    wave_vec(ii) = unique(data.wave(temp));

    cash_all(ii) = (1-temp_sum_w)*wealth_temp;
    
end

cash_1 = median(cash_all(wave_vec==1));
cash_2 = median(cash_all(wave_vec==2));

% compute allocations in the data

allocations_data = data.w_crypto_weighted.*data.wealth;

allocations_data_mean1 = zeros(length(unique_curr),1);
allocations_data_mean2 = zeros(length(unique_curr),1);

allocations_data_sum1 = zeros(length(unique_curr),1);
allocations_data_sum2 = zeros(length(unique_curr),1);

for ii=1:length(unique_curr)
    
    temp = (data.currency_id2==ii);
    
    allocations_data_mean1(ii) = mean(allocations_data(temp&data.wave==1));
    allocations_data_mean2(ii) = mean(allocations_data(temp&data.wave==2));
    
    allocations_data_sum1(ii) = sum(allocations_data(temp&data.wave==1));
    allocations_data_sum2(ii) = sum(allocations_data(temp&data.wave==2));
    
end

%% CF WITH RECENT FIRST-TIME BUYERS *REPLACED*

data.late = (strcmp(data.first_purchase_time,"2017 and later"));
% data.late = (strcmp(data.first_purchase_time,"2016-2017")|...
%              strcmp(data.first_purchase_time,"2017 and later"));

ind_nobuy_1 = (data.buyer==0&data.wave==1);
ID_unique_nobuy_1 = unique(data.household_id(ind_nobuy_1));

ind_nobuy_2 = (data.buyer==0&data.wave==2);
ID_unique_nobuy_2 = unique(data.household_id(ind_nobuy_2));

Nsim = 150;

p_eq_1_repl_sim = zeros(length(unique_curr),Nsim);
p_eq_2_repl_sim = zeros(length(unique_curr),Nsim);
mktcap_hat_1_repl_sim = zeros(length(unique_curr),Nsim);
mktcap_hat_2_repl_sim = zeros(length(unique_curr),Nsim);
cash_1_repl_sim = zeros(1,Nsim);
cash_2_repl_sim = zeros(1,Nsim);

short_increase_r_1 = zeros(1,Nsim);
short_decrease_r_1 = zeros(1,Nsim);
long_decrease_r_1 = zeros(1,Nsim);
potential_top3_r_1 = zeros(1,Nsim);
potential_other_r_1 = zeros(1,Nsim);

short_increase_r_2 = zeros(1,Nsim);
short_decrease_r_2 = zeros(1,Nsim);
long_decrease_r_2 = zeros(1,Nsim);
potential_top3_r_2 = zeros(1,Nsim);
potential_other_r_2 = zeros(1,Nsim);

for ss=1:Nsim
    
    data_replace = data;
    
    ind_latebuy_1 = (data.late==1&data.wave==1);
    ID_unique_latebuy_1 = unique(data.household_id(ind_latebuy_1));
    
    for ii=1:length(ID_unique_latebuy_1)
        
        temp_late = find(data.household_id==ID_unique_latebuy_1(ii));
        
        tempp = randi(length(ID_unique_nobuy_1));
        temp_nobuy = find(data.household_id==ID_unique_nobuy_1(tempp));
        
        data_replace.short_decrease(temp_late,:) = data.short_decrease(temp_nobuy,:);
        data_replace.short_increase(temp_late,:) = data.short_increase(temp_nobuy,:);
        data_replace.long_decrease(temp_late,:) = data.long_decrease(temp_nobuy,:);
        % data_replace.currencies_potential(temp_late,:) = data.currencies_potential(temp_nobuy,:);
                
        data_replace.household_id(temp_late) = data_replace.household_id(temp_late)/1000/ii; % make the id different to avoid duplication
        %ii
        
    end
       
    
    ind_latebuy_2 = (data.late==1&data.wave==2);
    ID_unique_latebuy_2 = unique(data.household_id(ind_latebuy_2));
    
    for ii=1:length(ID_unique_latebuy_2)
        
        temp_late = find(data.household_id==ID_unique_latebuy_2(ii));
        
        tempp = randi(length(ID_unique_nobuy_2));
        temp_nobuy = find(data.household_id==ID_unique_nobuy_2(tempp));
        
        data_replace.short_decrease(temp_late,:) = data.short_decrease(temp_nobuy,:);
        data_replace.short_increase(temp_late,:) = data.short_increase(temp_nobuy,:);
        data_replace.long_decrease(temp_late,:) = data.long_decrease(temp_nobuy,:);
        % data_replace.currencies_potential(temp_late,:) = data.currencies_potential(temp_nobuy,:);
                
        data_replace.household_id(temp_late) = data_replace.household_id(temp_late)/1000/ii; % make the id different to avoid duplication
        %ii
        
    end
    
    ID_unique_replace = unique(data_replace.household_id);
    
    fun1 = @(p) eq_p_fun(p,data_replace,c,scalefactor_wave1,supply_wave1,1,ID_unique_replace,unique_curr);
    fun2 = @(p) eq_p_fun(p,data_replace,c,scalefactor_wave2,supply_wave2,2,ID_unique_replace,unique_curr);
    
    p0_1 = zeros(length(unique_curr),1);
    for ii=1:length(unique_curr)
        temp = (data_replace.currency_id2==ii);
        p0_1(ii) = log(mean(data_replace.open(temp&data_replace.wave==1)));
    end
    [p_eq_1_repl_temp] = solve_contraction(fun1,p0_1);
    
    p0_2 = zeros(length(unique_curr),1);
    for ii=1:length(unique_curr)
        temp = (data_replace.currency_id2==ii);
        p0_2(ii) = log(mean(data_replace.open(temp&data_replace.wave==2)));
    end
    [p_eq_2_repl_temp] = solve_contraction(fun2,p0_2);
    
    %% counterfactual weights for the *REPLACE* case
    
    p_eq_repl_vec = NaN(size(data_replace,1),1);
    for ii=1:length(unique_curr)
        temp1 = (data_replace.currency_id2==ii&data_replace.wave==1);
        p_eq_repl_vec(temp1) = exp(p_eq_1_repl_temp(ii));
        temp2 = (data_replace.currency_id2==ii&data_replace.wave==2);
        p_eq_repl_vec(temp2) = exp(p_eq_2_repl_temp(ii));
    end
    
    ME_repl = data_replace.supply.*p_eq_repl_vec;
    
    dep_hat_repl = exp(c.logME*log(ME_repl) + ...
        c.short_increase*data_replace.short_increase + ...
        c.long_decrease*data_replace.long_decrease + ...
        c.currencies_potential*data_replace.currencies_potential + ...
        c.incomenewdummy*data_replace.incomenewdummy + c.agenewdummy*data_replace.agenewdummy + ...
        c.countrynewdummy*data_replace.countrynewdummy + ...
        c.type_objective*data_replace.type_objective +...
        c.log_libor*data_replace.log_libor + c.log_sp500*data_replace.log_sp500 + c.cons +...
        c.pow*data_replace.pow +...
        c.beta*data_replace.beta + c.momentum4*data_replace.momentum4);
    
    w_hat_repl = zeros(size(data_replace,1),1);
    
    for ii=1:length(ID_unique_replace)
        
        temp_ind = find(data_replace.household_id==ID_unique_replace(ii));
        
        for ccu=1:length(unique_curr)
            
            w_hat_repl(temp_ind(ccu)) = dep_hat_repl(temp_ind(ccu))./(1+nansum(dep_hat_repl(temp_ind)));
            
        end
    end
    
    mktcap_hat_repl_all = w_hat_repl.*data_replace.wealth;
    
    for ii=1:length(unique_curr)
        
        temp = (data_replace.currency_id2==ii);
        
        mktcap_hat_1_repl_sim(ii,ss) = nanmedian(mktcap_hat_repl_all(temp&data_replace.wave==1));
        mktcap_hat_2_repl_sim(ii,ss) = nanmedian(mktcap_hat_repl_all(temp&data_replace.wave==2));
        
    end
    
    % (mktcap_hat_1_repl-mktcap_hat_1)./mktcap_hat_1
    % (mktcap_hat_2_repl-mktcap_hat_2)./mktcap_hat_2
    
    
    % cash
    
    cash_repl_all = NaN(length(ID_unique_replace),1);
    wave_vec = NaN(length(ID_unique_replace),1);
    
    for ii=1:length(ID_unique_replace)
        
        temp = (data_replace.household_id==ID_unique_replace(ii));
        
        temp_sum_w = nansum(w_hat_repl(temp));
        
        wealth_temp = unique(data_replace.wealth(temp));
        wave_vec(ii) = unique(data_replace.wave(temp));
        
        cash_repl_all(ii) = (1-temp_sum_w)*wealth_temp;
        
    end
    
    cash_1_repl_sim(ss) = nanmedian(cash_repl_all(wave_vec==1));
    cash_2_repl_sim(ss) = nanmedian(cash_repl_all(wave_vec==2));
    
    p_eq_1_repl_sim(:,ss) = p_eq_1_repl_temp;
    p_eq_2_repl_sim(:,ss) = p_eq_2_repl_temp;
    
    % (cash_1_repl-cash_1)./cash_1
    % (cash_2_repl-cash_2)./cash_2
    
    %% beliefs
    
    short_increase_r_1(ss) = mean(data_replace.short_increase(data_replace.wave==1));
    short_decrease_r_1(ss) = mean(data_replace.short_decrease(data_replace.wave==1));
    long_decrease_r_1(ss) = mean(data_replace.long_decrease(data_replace.wave==1));
    potential_top3_r_1(ss) = mean(data_replace.currencies_potential((data_replace.currency_id2==2|...
        data_replace.currency_id2==5|...
        data_replace.currency_id2==8)&data_replace.wave==1));
    potential_other_r_1(ss) = mean(data_replace.currencies_potential((data_replace.currency_id2~=2&...
        data_replace.currency_id2~=5&...
        data_replace.currency_id2~=8)&data_replace.wave==1));
    
    short_increase_r_2(ss) = mean(data_replace.short_increase(data_replace.wave==2));
    short_decrease_r_2(ss) = mean(data_replace.short_decrease(data_replace.wave==2));
    long_decrease_r_2(ss) = mean(data_replace.long_decrease(data_replace.wave==2));
    potential_top3_r_2(ss) = mean(data_replace.currencies_potential((data_replace.currency_id2==2|...
        data_replace.currency_id2==5|...
        data_replace.currency_id2==8)&data_replace.wave==2));
    potential_other_r_2(ss) = mean(data_replace.currencies_potential((data_replace.currency_id2~=2&...
        data_replace.currency_id2~=5&...
        data_replace.currency_id2~=8)&data_replace.wave==2)); 
    
    ss
    
end

p_eq_1_repl = mean(p_eq_1_repl_sim,2);
p_eq_2_repl = mean(p_eq_2_repl_sim,2);
mktcap_hat_1_repl = mean(mktcap_hat_1_repl_sim,2);
mktcap_hat_2_repl = mean(mktcap_hat_2_repl_sim,2);
cash_1_repl = mean(cash_1_repl_sim,2);
cash_2_repl = mean(cash_2_repl_sim,2);

%% collect results

r.p_eq_col1 = exp(p_eq_1);
r.p_eq_col4 = exp(p_eq_1_repl);

r.allocations_col1 = mktcap_hat_1;
r.allocations_col4 = mktcap_hat_1_repl;

r = struct2dataset(r);

export(r,'File','../../output/tables/table7_col4_prices.csv','Delimiter',',')

% beliefs.short_decrease_1_data = mean(data.short_decrease(data.wave==1));
% beliefs.short_decrease_1_nor = mean(data_earlybuy.short_decrease(data_earlybuy.wave==1));
% beliefs.short_decrease_1_r = mean(short_decrease_r_1);

beliefs.short_increase_col1 = mean(data.short_increase(data.wave==1));
beliefs.short_increase_col4 = mean(short_increase_r_1);

beliefs.long_decrease_col1 = mean(data.long_decrease(data.wave==1));
beliefs.long_decrease_col4 = mean(long_decrease_r_1);

% beliefs.currencies_potential_top3_1_data = mean(data.currencies_potential((data.currency_id2==2|...
%     data.currency_id2==5|...
%     data.currency_id2==8)&data.wave==1));
% beliefs.currencies_potential_top3_1_nor = mean(data_earlybuy.currencies_potential((data_earlybuy.currency_id2==2|...
%     data_earlybuy.currency_id2==5|...
%     data_earlybuy.currency_id2==8)&data_earlybuy.wave==1));
% beliefs.currencies_potential_top3_1_r = mean(potential_top3_r_1);
% 
% beliefs.currencies_potential_other_1_data = mean(data.currencies_potential((data.currency_id2~=2&...
%     data.currency_id2~=5&...
%     data.currency_id2~=8)&data.wave==1));
% beliefs.currencies_potential_other_1_nor = mean(data_earlybuy.currencies_potential((data_earlybuy.currency_id2~=2&...
%     data_earlybuy.currency_id2~=5&...
%     data_earlybuy.currency_id2~=8)&data_earlybuy.wave==1));
% beliefs.currencies_potential_other_1_r = mean(potential_other_r_1);
% 
% 
% 
% beliefs.short_decrease_2_data = mean(data.short_decrease(data.wave==2));
% beliefs.short_decrease_2_nor = mean(data_earlybuy.short_decrease(data_earlybuy.wave==2));
% beliefs.short_decrease_2_r = mean(short_decrease_r_2);
% 
% beliefs.short_increase_2_data = mean(data.short_increase(data.wave==1));
% beliefs.short_increase_2_nor = mean(data_earlybuy.short_increase(data_earlybuy.wave==1));
% beliefs.short_increase_2_r = mean(short_increase_r_1);
% 
% beliefs.long_decrease_2_data = mean(data.long_decrease(data.wave==2));
% beliefs.long_decrease_2_nor = mean(data_earlybuy.long_decrease(data_earlybuy.wave==2));
% beliefs.long_decrease_2_r = mean(long_decrease_r_2);

% beliefs.currencies_potential_top3_2_data = mean(data.currencies_potential((data.currency_id2==2|...
%     data.currency_id2==5|...
%     data.currency_id2==8)&data.wave==2));
% beliefs.currencies_potential_top3_2_nor = mean(data_earlybuy.currencies_potential((data_earlybuy.currency_id2==2|...
%     data_earlybuy.currency_id2==5|...
%     data_earlybuy.currency_id2==8)&data_earlybuy.wave==2));
% beliefs.currencies_potential_top3_2_r = mean(potential_top3_r_2);
% 
% beliefs.currencies_potential_other_2_data = mean(data.currencies_potential((data.currency_id2~=2&...
%     data.currency_id2~=5&...
%     data.currency_id2~=8)&data.wave==2));
% beliefs.currencies_potential_other_2_nor = mean(data_earlybuy.currencies_potential((data_earlybuy.currency_id2~=2&...
%     data_earlybuy.currency_id2~=5&...
%     data_earlybuy.currency_id2~=8)&data_earlybuy.wave==2));
% beliefs.currencies_potential_other_2_r = mean(potential_other_r_2);

beliefs = struct2dataset(beliefs);

export(beliefs,'File','../../output/tables/table7_col4_beliefs.csv','Delimiter',',')
